package com.ming.javaweb.servlet;

import com.ming.javaweb.beans.entity.AdminDO;
import com.ming.javaweb.service.AdminService;
import com.ming.javaweb.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("trueLogin".equals(type)){
            String userName = req.getParameter("userName");
            System.out.println("传入用户名："+userName);
            String pwd = req.getParameter("pwd");

            if ("".equals(userName) || "".equals(pwd) || userName==null || pwd==null){
                req.setAttribute("message","用户名或密码不能为空");
                req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);
                return;
            }

            AdminServiceImpl adminService = new AdminServiceImpl();

            AdminDO adminDO = null;
            try {
                adminDO = adminService.validateLogin(userName);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (adminDO != null && pwd.equals(adminDO.getPwd())){
//                放到session之前清空敏感信息
                adminDO.setPwd(null);
                req.getSession().setAttribute("adminDo",adminDO);
//                当我门做了提交表单的动作后再通过getRequestDispatcher进行内部转发，
//                刷新页面会导致表单的重复提交
//                所以使用重定向
                resp.sendRedirect(req.getContextPath()+"/studentServlet?type=toStudentManage");
            }else {
                req.setAttribute("message","用户名或密码错误");
                req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);
            }
        }else if ("register".equals(type)){

            String userName = req.getParameter("userName");
            String pwd = req.getParameter("pwd");

            if ("".equals(userName)||"".equals(pwd)||userName==null||pwd==null) {
                req.setAttribute("message","用户名或密码不能为空");
                req.getRequestDispatcher("register.jsp").forward(req,resp);
                return;
            }
            //存值到DO层
            AdminDO adminDO = new AdminDO();
            adminDO.setUserName(userName);
            adminDO.setPwd(pwd);

            try {
                adminService.register(adminDO);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);

        }else {
            req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);
        }
    }
}
